from django.conf import settings
from itsdangerous import TimedJSONWebSignatureSerializer


def generate_access_token(openid):
    obj = TimedJSONWebSignatureSerializer(settings.SECRET_KEY, expires_in=600)
    dict = {'openid': openid}
    return obj.dumps(dict).decode()


def check_access_token(access_token):
    obj = TimedJSONWebSignatureSerializer(settings.SECRET_KEY, expires_in=600)

    try:
        data = obj.loads(access_token)

    except Exception as e:
        return None

    else:
        return data.get('openid')
